<template>
  <div >
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="850px"
      :before-close="tableClose"
    >
      <el-form
        :model="addInfo"
        status-icon
        :rules="rules"
        ref="addInfoRef"
        label-width="70px"
      >
        <el-row>
          <el-col :span="7">
            <el-form-item label="基地ID" prop="shipmentEntryBillDto.baseId">
              <el-input
                v-model="addInfo.shipmentEntryBillDto.baseId"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item label="类型" prop="shipmentEntryBillDto.type">
              <el-input
                v-model="addInfo.shipmentEntryBillDto.type"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7" :offset="1">
            <el-form-item label="经手人"
             prop="shipmentEntryBillDto.handler"
            :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }">
              <el-input
                v-model="addInfo.shipmentEntryBillDto.handler"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="22">
        <el-form-item label="备注" prop="shipmentEntryBillDto.remark">
          <el-input
            type="textarea"
            :rows="1"
            v-model="addInfo.shipmentEntryBillDto.remark"
          ></el-input>
        </el-form-item>
        </el-col>
        </el-row>
        <!-- 需要新增多个订单详情 -->
        <el-divider content-position="center">订单详情</el-divider>
        <el-form-item
          v-for="(item, index) in addInfo.shipmentEntryBaseDtoList"
          :label="'类型' + (index + 1)"
          :key="item.key "
          label-width='50px'
        >
          <el-row>
            <el-col :span="5" >
              <el-form-item label="名称"
               :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }"
              :prop="
              'shipmentEntryBaseDtoList.' + index + '.typeName'
              ">
                <el-input
                placeholder="请输入"
                v-model="item.typeName"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" >
              <el-form-item label="数量"
               :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }"
              :prop="
              'shipmentEntryBaseDtoList.' + index + '.amount'
              ">
                <el-input
                  placeholder="请输入"
                  v-model="item.amount"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" >
              <el-form-item label="单价"
               :rules="{
                      required: true,
                      message: '不能为空',
                      trigger: 'blur',
                    }"
              :prop="
              'shipmentEntryBaseDtoList.' + index + '.unitPrice'
              ">
                <el-input
                  placeholder="请输入"
                  v-model="item.unitPrice"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="5" >
              <el-form-item label="备注"
              :prop="
              'shipmentEntryBaseDtoList.' + index + '.remark'
              ">
                <el-input
                  placeholder="请输入"
                  v-model="item.remark"
                ></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="1" >
              <el-form-item >
                <el-button
              @click.prevent="removeDomain(item)"
              style="margin-left: 40px"
              size="mini"
              type="warning"
              >删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
          <el-button   @click="addDomain"
          type="info" plain>新增批次详情</el-button>
        <el-button @click="closeform" >取 消</el-button>
        <el-button type="primary" @click="addOrderInfo('addInfo')">提 交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { AddOrder } from "@/api/Dovecote/StoreManage/Materialln.js";

export default {
  name: "OrderForm",
  props: {
    addInfo: {
      Object,
      default: {},
    },
    dialogVisible: {
      Boolean,
      default: false,
    },
    //表名
    title: {
      String,
      default: "",
    },
  },
  data() {
    return {

      //表单规则
      rules: {
      },
    };
  },
  methods: {
    // //查询表格数据
    DisplayData() {
      this.$emit("DisplayData");
    },
    // 添加订单
    addOrderInfo() {
      this.$refs["addInfoRef"].validate(async (valid) => {
        if (valid) {
          console.log("新增提交的订单", this.addInfo);
          const { data: res } = await AddOrder(this.addInfo);
          console.log('fang',res);
          if (res.statusCode === 20000) {
            this.$message.success("增加成功");
           this.closeform();
          } else {
            if (res.data == null) {
              return this.$message.error("添加失败");
            }
          }
        }
      });
    },

    //删除记录
    removeDomain(item) {
      var index = this.addInfo.shipmentEntryBaseDtoList.indexOf(item);
      if (index !== -1) {
        this.addInfo.shipmentEntryBaseDtoList.splice(index, 1);
      }
    },
    //弹窗新增详情记录
    addDomain() {
      this.addInfo.shipmentEntryBaseDtoList.push({
        type: "种鸽",
        typeName: "",
        unitPrice: "",
        amount: "",
        remark: "",
      });
    },

    //重置
    resetForm() {
      this.$nextTick(() => {
        this.$refs.addInfoRef.resetFields();
      });
    },
    //关闭表单
    closeform() {
      var a = false;
      this.$emit("closeAddDialog", a);
      this.DisplayData();
      this.resetForm();
    },

    // 关闭弹窗
    tableClose() {
      this.$confirm("确认关闭？")
        .then(() => {
          this.closeform();
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>
